#include "QHMainWindow.h"
#include "ui_QHMainWindow.h"

#include <QFileDialog>
#include <QStandardPaths>
#include <QTime>
#include <QDir>
#include <QDebug>

QHMainWindow::QHMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::QHMainWindow)
{
    ui->setupUi(this);
    m_vlcPlayer=new QHVlcPlayer(this);

    ui->volumeSlider->setValue(50);
    ui->volumeSlider->setRange(0,100);

    connect(m_vlcPlayer,&QHVlcPlayer::signalPositonChanged,this,&QHMainWindow::slotPositionChanged);
    connect(m_vlcPlayer,&QHVlcPlayer::signalTimeChanged,this,&QHMainWindow::slotTimeChanged);
    connect(m_vlcPlayer,&QHVlcPlayer::signalMediaPlaying,this,&QHMainWindow::slotMediaPlaying);
    connect(m_vlcPlayer,&QHVlcPlayer::signalMediaError,this,&QHMainWindow::slotMediaError);
    connect(ui->progressSlider,&QSlider::sliderMoved,this,&QHMainWindow::slotProgressSliderMoved);
    connect(ui->volumeSlider,&QSlider::sliderMoved,this,&QHMainWindow::slotVolumeSliderMoved);
}

QHMainWindow::~QHMainWindow()
{
    delete ui;
}

void QHMainWindow::on_openButton_clicked()
{
    QString fileName;
    fileName = QFileDialog::getOpenFileName(this,"Open File","","Video File(*.mp4 *.avi *.mkv)");
    if(fileName.isEmpty())
    {
        return;
    }
    else
    {
        fileName=QDir::toNativeSeparators(fileName);
        m_vlcPlayer->setLocalFile(fileName,(void *)ui->videoWidget->winId());
        on_playButton_clicked();
    }
}

void QHMainWindow::on_playButton_clicked()
{
    if(ui->playButton->text()=="Play")
    {
        m_vlcPlayer->play();
        ui->playButton->setText("Pause");
    }
    else if(ui->playButton->text()=="Pause")
    {
        m_vlcPlayer->pause();
        ui->playButton->setText("Play");
    }
}

void QHMainWindow::on_stopButton_clicked()
{
    m_vlcPlayer->stop();
    ui->timeLabel->setText("00:00:00/00:00:00");
    ui->progressSlider->setValue(0);
    ui->playButton->setText("Play");
}

void QHMainWindow::on_startButton_clicked()
{
     m_vlcPlayer->setUrl(ui->lineEdit->text(),(void *)ui->videoWidget->winId());
     on_playButton_clicked();
}

void QHMainWindow::slotProgressSliderMoved(int value)
{
    int total=ui->progressSlider->maximum();
    m_vlcPlayer->seekTo(value*1.0/total);
    long long totalTime=m_vlcPlayer->getTotalTime();
    long long currentTime = totalTime*value/total;
    ui->timeLabel->setText(formatTime(currentTime)+"/"+formatTime(totalTime));
}

void QHMainWindow::slotPositionChanged(float pos)
{
    int total=ui->progressSlider->maximum();
    ui->progressSlider->setValue(pos*total);
}

void QHMainWindow::slotTimeChanged(long long msec)
{
    long long totalTime=m_vlcPlayer->getTotalTime();
    long long currentTime = msec;
    ui->timeLabel->setText(formatTime(currentTime)+"/"+formatTime(totalTime));
}

void QHMainWindow::slotMediaPlaying()
{
    ui->progressSlider->setValue(0);
    ui->progressSlider->setRange(0,m_vlcPlayer->getTotalTime());
    m_vlcPlayer->setVolume(ui->volumeSlider->value());
}

void QHMainWindow::slotMediaError()
{
    qDebug()<<"libvlc_MediaPlayerEncounteredError";
}

void QHMainWindow::sleep(int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
    QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
}

QString QHMainWindow::formatTime(long long time)
{
    int t = time / 1000;
    int hh = t / 3600;
    int mm = (t % 3600) / 60;
    int ss = t % 60;

    char str[128]={'\0'};
    sprintf(str, "%02d:%02d:%02d", hh, mm, ss);
    return QString(str);
}

void QHMainWindow::slotVolumeSliderMoved(int value)
{
    m_vlcPlayer->setVolume(value);
}

